package com.RunAndHappy.q779178.server.controller;

import com.RunAndHappy.q779178.pojo.entity.User;
import com.RunAndHappy.q779178.server.service.UserService;
import com.RunAndHappy.q779178.utils.Resp;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stats")
public class UserController {
    ObjectMapper mapper = new ObjectMapper(); // create once, reuse

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public String loginResult(@RequestParam String uname, @RequestParam String password) throws JsonProcessingException {

        User user = new User();
        user=userService.loginService(uname, password);
        if(user!=null){
            return mapper.writeValueAsString(user);
        }
        return "err";
    }
    @PostMapping("/register")
    public String registResult(@RequestBody User newUser){
        User user = userService.registService(newUser);
        if(user!=null){

            return "OKR";

        }else{

            return "ext";

        }
    }
}
